package com.yh.patterns.Iterator.job;

import java.util.*;

/**
 * @Author:yh
 * @Description:
 */
public class IteratorDemo {
    public static void main(String[] args) {
        //创建集合对象
        List<Student> list = new ArrayList();
        //创建学生对象
        Student s1 = new Student("张三",25);
        Student s2 = new Student("李四",16);
        Student s3 = new Student("王五",20);
        Student s4 = new Student("赵六",80);
        //把学生对象添加到集合对象中
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
       //降序排列
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });
        //通过集合对象获取迭代器对象
        Iterator it = list.iterator();
        //输出
        while(it.hasNext()) {
            //通过迭代器对象的next方法获取元素
            Student s = (Student) it.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

